package com.itcrowd.ittao.view;

import java.util.ArrayList;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.itcrowd.ittao.R;
import com.itcrowd.ittao.service.ShoppingListChangedListener;
import com.itcrowd.ittao.service.ShoppingListManager;
import com.itcrowd.ittao.service.data.Price;
import com.itcrowd.ittao.service.data.Product;
import com.itcrowd.ittao.service.data.ProductGroup;
import com.itcrowd.ittao.service.data.ProductStatus;
import com.itcrowd.ittao.utils.BitmapCache;
import com.itcrowd.ittao.utils.ImageManager;
import com.itcrowd.ittao.widget.AbstractStickyHeaderExpandableListViewAdapter;
import com.itcrowd.ittao.widget.StickyHeaderExpandableListView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ShoppingListFragment extends Fragment implements ShoppingListChangedListener, OnChildClickListener {

	private ShoppingListAdapter mAdapter;
	private StickyHeaderExpandableListView mListView;
	private LayoutInflater mInflater;
	private ShoppingListManager<ShoppingListChangedListener> mShoppingListManager = ShoppingListManager.getInstance();
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		activity.getActionBar().setTitle(R.string.shoppinglist);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mInflater = inflater;
		View view = inflater.inflate(R.layout.shoppinglist_fragment, container, false);
		
		mAdapter = new ShoppingListAdapter();
		mListView = (StickyHeaderExpandableListView) view.findViewById(R.id.shoppinglist);
		mListView.setAdapter(mAdapter);
		mListView.setOnChildClickListener(this);
		
		View emptyView = view.findViewById(R.id.empty_view);
		mListView.setEmptyView(emptyView);
		mShoppingListManager.registerObserver(this);
		mAdapter.setShoppingList(mShoppingListManager.getShoppingList());
		
		return view;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.shoppinglist_menu, menu);
	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.scan:
			startActivity(new Intent(getActivity(), CaptureActivity.class));
			break;
		case R.id.refresh:
			mShoppingListManager.retrieveShoppingList();
			break;
		}
		return true;
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mShoppingListManager.unregisterObserver(this);
	}
	
	public class ShoppingListAdapter extends AbstractStickyHeaderExpandableListViewAdapter {

		private ArrayList<ProductGroup> mShoppingList;
		
		public ShoppingListAdapter() {
			
		}
		
		public void setShoppingList(ArrayList<ProductGroup> list) {
			mShoppingList = list;
			notifyDataSetChanged();
		}
		
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return mShoppingList.get(groupPosition).getProducts().get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			Product product = (Product) getChild(groupPosition, childPosition);
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.shoppinglist_product_item, null, false);
			}
			NetworkImageView img = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
			ImageManager.setImageUrl(img, R.drawable.default_img, product.getPictureUrl());
			
			TextView name = (TextView) convertView.findViewById(R.id.name);
			ImageView statusImg = (ImageView) convertView.findViewById(R.id.purchase_status_image);
			TextView des = (TextView) convertView.findViewById(R.id.description);
			TextView price = (TextView) convertView.findViewById(R.id.price);
			TextView status = (TextView) convertView.findViewById(R.id.status);
			name.setText(product.getName());
			if(product.getProductStatus().getCurrentNumber() == 0) {
				statusImg.setImageResource(R.drawable.red_dot);
			} else if(product.getProductStatus().getCurrentNumber() < product.getProductStatus().getTotalNumber()) {
				statusImg.setImageResource(R.drawable.yellow_dot);
			} else {
				statusImg.setImageResource(R.drawable.green_dot);
			}
			des.setText(product.getQRCode());
			price.setText(product.getPrice().toString());
			ProductStatus productStatus = product.getProductStatus();
			status.setText(productStatus.getCurrentNumber()+"/"+productStatus.getTotalNumber());
			convertView.setTag(product);
			
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mShoppingList.get(groupPosition).getProducts().size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return mShoppingList.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return mShoppingList == null ? 0 : mShoppingList.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			ProductGroup group = (ProductGroup) getGroup(groupPosition);
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.shoppinglist_group_item, null, false);
			}
			ImageView indicator = (ImageView) convertView.findViewById(R.id.groupindicator);
			TextView groupName = (TextView) convertView.findViewById(R.id.category);
			if(isExpanded) {
				indicator.setImageResource(R.drawable.group_indicator_open);
			} else {
				indicator.setImageResource(R.drawable.group_indicator_close);
			}
			groupName.setText(group.getProductGroupName());
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public View getHeaderView() {
			View view = mInflater.inflate(R.layout.shoppinglist_group_item, null, false);
			ImageView indicator = (ImageView) view.findViewById(R.id.groupindicator);
			indicator.setImageResource(R.drawable.group_indicator_open);
			return view;
		}

		@Override
		public void updateHeaderViewContent(View headerView, int groupPos) {
			ProductGroup group = (ProductGroup) getGroup(groupPos);
			((TextView) headerView.findViewById(R.id.category)).setText(group.getProductGroupName());
		}

		@Override
		public void updateGroupViewVisible(View groupView, boolean visible) {
			int visibility = visible ? View.VISIBLE : View.INVISIBLE;
			groupView.findViewById(R.id.groupindicator).setVisibility(visibility);
			groupView.findViewById(R.id.category).setVisibility(visibility);
		}
		
	}

	@Override
	public void onShoppingListChanged() {
		mAdapter.setShoppingList(mShoppingListManager.getShoppingList());
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Product product = (Product) v.getTag();
		Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
		intent.putExtra("product", product.getQRCode());
		startActivity(intent);
		return true;
	}
}
